package orbito.physics.motion;

import java.util.Collection;
import java.util.LinkedList;

import orbito.physics.PhysicsSettings;
import orbito.util.Vec2;

public class GravitationalForce extends AbstractForce {
	Actor body1, body2;
	double g;
	
	public GravitationalForce(Actor body1, Actor body2) {
		this(body1, body2, PhysicsSettings.gravitationalCoefficient);
	}
	
	public GravitationalForce(Actor body1, Actor body2, double g) {
		this.body1 = body1;
		this.body2 = body2;
		this.g = g;
	}
	
	public void applyForces() {
		Vec2 d = body2.getPosition().sub(body1.getPosition());
		double r2 = d.lengthSquared();
		if (r2 > 0) {
			Vec2 f = d.iunit().mul(g * body1.getMass() * body2.getMass() / r2);
			body1.applyForce(f);
			body2.applyForce(f.inegate());
		}
	}
	
	public Collection<Actor> getActors() {
		Collection<Actor> actors = new LinkedList<Actor>();
		actors.add(body1);
		actors.add(body2);
		return actors;
	}
}
